package com.suman.bbcticker.weather;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.suman.bbcticker.ApplicationStateObjectsHolder;
import com.suman.bbcticker.configure.RssFeedPreference;

public class GoogleWeatherService implements LocationListener {
	public enum WeatherAction {
		RETRIEVE, REFRESH
	};

	// TODO minimum time might be the same as feedrefreshtime??
	// Distance...Still bit confused abt distance??may be 1k

	private static final String TAG = "GoogleWeatherService";
	private static final int LOCATION_UPDATE_INTERVAL_MILLIS = 60000;
	private static final int LOCATION_UPDATE_DISTANCE_METERS = 1000;
	private Context mContext;
	private Integer mAppWidgetId;
	private Handler mHandler;

	public LocationListener handleWeather(Context context, Integer appWidgetId) {
		this.mHandler = new Handler();
		this.mContext = context;
		this.mAppWidgetId = appWidgetId;
		LocationManager mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_UPDATE_INTERVAL_MILLIS, LOCATION_UPDATE_DISTANCE_METERS, this, Looper.getMainLooper());
		mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LOCATION_UPDATE_INTERVAL_MILLIS, 0, this,Looper.getMainLooper());
		return this;
	}

	public void onLocationChanged(final Location location) {
		Log.e(TAG, "OnLocation Changed Called..");
		mHandler.post(new Runnable() {

			public void run() {
				try {
					Log.e(TAG, "OnLocation Changed Called..Running inside thread");
					Double latitude;
					Double longitude;

					RssFeedPreference rssFeedPreference = ApplicationStateObjectsHolder.preferenceHashMap.get(mAppWidgetId);
					List<Address> addressList = new ArrayList<Address>();
					// Based on user preferences,call getFromLocation() method
					// use latitude from address
					Geocoder geocoder = new Geocoder(mContext);
					if (rssFeedPreference.isGpsWeatherSelection()) {
						addressList = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
						latitude = location.getLatitude();
						longitude = location.getLongitude();
						
						
					} else {
						String postcode = rssFeedPreference.getCustomLocationWeatherSelectionValue();
						addressList = geocoder.getFromLocationName(postcode, 1);
						Address address = addressList.get(0);
						latitude = address.getLatitude();
						longitude = address.getLongitude();
					}

					Address address = addressList.get(0);
					String locationDetail = address != null ? address.getAddressLine(0) : "";
					Log.e(TAG, "Locationdetail is..."+ locationDetail);
					new WeatherUtils().invokeGoogleWeatherServiceAndUpdateWidget(latitude, longitude, mContext, mAppWidgetId, locationDetail);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}

		});
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
